"""
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License. 
"""

from zope.interface import implements

from meercat.interfaces import IInstance, IMetadata, IPickleable

class Metadata(object):
    """ A basic implementation of the IMetadata interface. All data is stored
    as attributes on the object. """
    implements(IMetadata, IPickleable)
    
    _format = None
    _source = None
    _value  = None
    
    def __init__(self,format=None,source=None,value=None,instance=None):
        self._format = format
        self._source = source
        self._value  = value
        
        if instance is not None:
            self._instance = IPickleable(IInstance(instance))
        else:
            self._instance = None
    
    def __eq__(self, other):
        if not IMetadata.providedBy(other):
            return False
        
        return (other.getSource() == self.getSource() and
                other.getFormat() == self.getFormat() and
                other.getValue() == self.getValue())
    
    def __hash__(self):
        return hash((self.getSource(),self.getFormat(),self.getValue(),)) 
    
    def __unicode__(self):
        return u'Metadata(source=%s, format=%s, value=%s)' % (str(self._source),
                                                              str(self._format),
                                                              str(self._value))
    
    def getFormat(self):
        return self._format
    
    def getSource(self):
        return self._source
    
    def getValue(self):
        return self._value
    
    def getInstance(self):
        return self._instance
    
    def setInstance(self, instance):
        self._instance = instance